package com.cms.admin.service.common.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;


import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.cms.admin.service.common.TemplateService;

import freemarker.template.Configuration;

public class TemplateServiceImpl implements TemplateService {

	
	 private Configuration configuration;
	
	@Override
	public String generateContentByFreeMarker(String templateName, Map model)
			throws Exception {
		// TODO Auto-generated method stub
		  return FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(templateName,"UTF-8"), model);

	}
	
	@Override
	public void processCreateHtml(String templateName, Map root, File file) throws Exception {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		Writer out = null;
			file.createNewFile();												//创建文件对象
			fos = new FileOutputStream(file);
			osw = new OutputStreamWriter(fos, "UTF-8");	//, "UTF-8"
            out = new BufferedWriter(osw);		//文件流
            configuration.getTemplate(templateName).process(root, out);											//解析模板引擎
            out.flush();
			osw.close();
			out.close();
			fos.close();
	}
		
	
    public Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

}
